
export SDKVERSION = 1.3.0

export HOST
export PY

ifneq ($(COMPILER_PATH),)
COMPILER_BIN_PATH = $(COMPILER_PATH)/bin/
endif

export CC = $(COMPILER_BIN_PATH)arm-none-eabi-gcc
export AR = $(COMPILER_BIN_PATH)arm-none-eabi-ar
export LD = $(COMPILER_BIN_PATH)arm-none-eabi-gcc
export OBJCOPY = $(COMPILER_BIN_PATH)arm-none-eabi-objcopy

V = 0
ifeq ($(V),1)
export Q :=
else
ifeq ($(V),0)
export Q := @
endif
endif

export APP_ENTRY := 

export ROOT_PATH := ../

export BUILD_ROOT_PATH := build
export BUILD_APP_PATH := $(BUILD_ROOT_PATH)/$(APP_ENTRY)
export BUILD_TMP_DIR := temp
export BUILD_OBJ_DIR := object
export BUILD_BIN_DIR := binary

export BUILD_TMP_PATH := $(BUILD_APP_PATH)/$(BUILD_TMP_DIR)
export BUILD_OBJ_PATH := $(BUILD_APP_PATH)/$(BUILD_OBJ_DIR)
export BUILD_BIN_PATH := $(BUILD_APP_PATH)/$(BUILD_BIN_DIR)

export GLOBAL_INCS_TMP_FILE := global_incs.tmp
export GLOBAL_DEFINE_TMP_FILE := global_define.tmp
export ARCHIVES_RECORD_TMP_FILE := archives_record.tmp
export USER_ARCHIVES_RECORD_TMP_FILE := user_archives_record.tmp

APP_RECORD_TMP_FILE := app_record.tmp
FW_VER_RECORD_TMP_FILE := fw_ver_record.tmp
BOARD_RECORD_TMP_FILE := board_record.tmp
DFLAGS_TMP_FILE := dflags.tmp

define READ_APP_RECORD_TMP_FILE
$(shell cat $(BUILD_TMP_PATH)/$(APP_RECORD_TMP_FILE) 2> /dev/null)
endef

define READ_FW_VER_RECORD_TMP_FILE
$(shell cat $(BUILD_TMP_PATH)/$(FW_VER_RECORD_TMP_FILE) 2> /dev/null)
endef

define READ_BOARD_RECORD_TMP_FILE
$(shell cat $(BUILD_TMP_PATH)/$(BOARD_RECORD_TMP_FILE) 2> /dev/null)
endef

define READ_DFLAGS_TMP_FILE
$(shell sed -n '/\"/p' $(BUILD_TMP_PATH)/$(DFLAGS_TMP_FILE)  | sed 's/\"/\\\"/g' 2> /dev/null)
endef

NAME := TOP

export FW_NAME = $(notdir $(APP_ENTRY))

export TARGET_BIN = $(BUILD_BIN_PATH)/$(FW_NAME).bin
export TARGET_HEX = $(BUILD_BIN_PATH)/$(FW_NAME).hex
export TARGET_ELF = $(BUILD_BIN_PATH)/$(FW_NAME).elf
export TARGET_MAP = $(BUILD_BIN_PATH)/$(FW_NAME).map

export PLAT = stm32f10x
export BOARD = ELITE
include plat.mk

-include $(APP_ENTRY)/user_cfg.mk

export GLOBAL_INCS :=
export GLOBAL_DEFINE :=

ifeq ($(CFLAGS_USER),)
CFLAGS_USER = -W -Wall -Wno-sign-compare -Wno-pointer-sign -Wno-type-limits -Wno-unused-function -ffunction-sections -fdata-sections -g -O2
endif

ifneq ($(shell ls menuconfig/autoconf.h 2> /dev/null),)
AUTOCONF_HEADER = menuconfig/autoconf.h
CFLAGS_INC_HEADER = --include=$(AUTOCONF_HEADER)
endif

export CFLAGS := $(CFLAGS_USER) $(CHIP_CFLAGS) $(CFLAGS_INC_HEADER) -std=gnu11

DFLAGS += $(DFLAGS_USER)
ifeq ($(WITH_OS), n)
DFLAGS += PLAT_NO_OS
endif
DFLAGS += \
	PLAT_$(strip $(PLAT)) BOARD_$(strip $(BOARD)) FW_NAME=\"$(strip $(FW_NAME))\" \
	BUILDDATE=\"$(shell date +%Y-%m-%d)\" \
	SDKVERSION=\"$(SDKVERSION)\" \
	PLATNAME=\"$(PLAT)\" \
	BOARDNAME=\"$(BOARD)\"
export DFLAGS

ifeq ($(USE_NANO_LIB), y)
LDFLAGS_USE_NANO_LIB = -specs=nano.specs
endif
ifneq ($(USE_NOSYS_LIB), n)
LDFLAGS_USE_NOSYS_LIB = -specs=nosys.specs
endif
export LDFLAGS := $(CHIP_LDFLAGS) \
				  -Wl,--start-group -lc -lm -Wl,--end-group \
				  $(LDFLAGS_USE_NANO_LIB) $(LDFLAGS_USE_NOSYS_LIB) -static -Wl,-cref,-u,Reset_Handler \
				  -Wl,-Map=$(TARGET_MAP) -Wl,--gc-sections \
				  -Wl,--wrap=malloc -Wl,--wrap=calloc -Wl,--wrap=realloc -Wl,--wrap=free \
				  -Wl,--wrap=fflush -Wl,--wrap=fclose

make_mk_fnc = $(1)/$(notdir $(1)).mk

$(NAME)_COMPONENTS = $(APP_ENTRY)

$(NAME)_MK = $(foreach n, $($(NAME)_COMPONENTS), $(call make_mk_fnc, $(n)))

.PHONY : target construct prepare update_tmp_file exec end flash clean clean_pre_msg clean_post_msg clean_$(CLEAN_TARGET)

target: construct end

construct: prepare update_tmp_file exec

prepare:
ifeq ($(APP_ENTRY),)
	$(error Please specify an application directory !!!)
endif
ifneq ($(READ_BOARD_RECORD_TMP_FILE), $(BOARD))
	@-rm -rf $(BUILD_APP_PATH)
else
ifneq ($(strip $(sort $(READ_DFLAGS_TMP_FILE))), $(strip $(sort $(DFLAGS))))
	@-rm -rf $(BUILD_OBJ_PATH)/*
endif
ifneq ($(READ_APP_RECORD_TMP_FILE), $(APP_ENTRY))
	@-rm -rf $(BUILD_OBJ_PATH)/$(READ_APP_RECORD_TMP_FILE)
	@-rm -rf $(BUILD_BIN_PATH)/*
endif
ifneq ($(READ_FW_VER_RECORD_TMP_FILE), $(FW_NAME))
	@-rm -rf $(BUILD_BIN_PATH)/*
endif
endif
	@mkdir -p $(BUILD_TMP_PATH) $(BUILD_OBJ_PATH) $(BUILD_BIN_PATH)

update_tmp_file:
	@echo $(APP_ENTRY) > $(BUILD_TMP_PATH)/$(APP_RECORD_TMP_FILE)
	@echo $(FW_NAME) > $(BUILD_TMP_PATH)/$(FW_VER_RECORD_TMP_FILE)
	@echo $(BOARD) > $(BUILD_TMP_PATH)/$(BOARD_RECORD_TMP_FILE)
	@echo $(DFLAGS) > $(BUILD_TMP_PATH)/$(DFLAGS_TMP_FILE)

exec:
	$(Q)$(PY) compile.py $(APP_ENTRY)
	@echo

link:
	@echo
	@make --no-print-directory -f link.mk $(TARGET_BIN)
	@make --no-print-directory -f link.mk $(TARGET_HEX)
	@echo

end:
	@echo ====================================================
	@echo ============ Project construct finished ============
	@echo ====================================================

flash:
	$(Q)make --no-print-directory -f flash.mk

clean: clean_pre_msg clean_$(CLEAN_TARGET) clean_post_msg

clean_pre_msg:
	@echo ----- cleaning $(BUILD_ROOT_PATH)/$(CLEAN_TARGET)...

clean_post_msg:
	@echo ----- done.

clean_$(CLEAN_TARGET):
	@-rm -rf $(BUILD_ROOT_PATH)/$(CLEAN_TARGET)

export KCONFIG_ENTRY = menuconfig/config.in
export KCONFIG_CONFIG = menuconfig/config.mk
export KCONFIG_AUTOHEADER = menuconfig/autoconf.h
mcfg:
	@$(PY) -c "from kconfiglib import Kconfig;from menuconfig import menuconfig;kconf=Kconfig(filename=\"$(KCONFIG_ENTRY)\");menuconfig(kconf);kconf.write_autoconf();"